﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;

namespace Wulong.Phone.Dianping.Utils
{
    public static class WebBrowserHelper
    {
        public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
            "Html", typeof(string), typeof(WebBrowserHelper), new PropertyMetadata(OnHtmlChanged));

        public static readonly DependencyProperty SourceProperty = DependencyProperty.RegisterAttached(
            "Source", typeof(string), typeof(WebBrowserHelper), new PropertyMetadata(OnSourceChanged));

        public static string GetHtml(DependencyObject dependencyObject)
        {
            return (string)dependencyObject.GetValue(HtmlProperty);
        }

        public static void SetHtml(DependencyObject dependencyObject, string value)
        {
            dependencyObject.SetValue(HtmlProperty, value);
        }

        private static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var browser = d as WebBrowser;
            if (browser == null)
                return;
            
            var html = e.NewValue.ToString();
            try
            {
                browser.NavigateToString(html);
            }
            catch (Exception)
            {
                browser.Loaded += (s, e3) => browser.NavigateToString(html);
            }
        }

        public static string GetSource(DependencyObject dependencyObject)
        {
            return (string)dependencyObject.GetValue(SourceProperty);
        }

        public static void SetSource(DependencyObject dependencyObject, string value)
        {
            dependencyObject.SetValue(SourceProperty, value);
        }

        private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var browser = d as WebBrowser;
            if (browser == null)
                return;
            var source = e.NewValue.ToString();
            browser.Navigate(new Uri(source));
        }


        public static string HtmlHeader(double viewportWidth)
        {
            var head = new StringBuilder();     
            head.Append("<head>");    
            head.Append(string.Format(        
                "<meta name=\"viewport\" value=\"width={0}\" user-scalable=\"no\">",        
                viewportWidth));
            head.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            head.Append("<style>");    
            head.Append("html { -ms-text-size-adjust:150% }");    
            head.Append(string.Format(        
                "body {{background:{0};color:{1};font-family:'Segoe WP';font-size:{2}pt;margin:0;padding:0 }}",        
                GetBrowserColor("PhoneBackgroundColor"),        
                GetBrowserColor("PhoneForegroundColor"),        
                (double)Application.Current.Resources["PhoneFontSizeNormal"]));    
            head.Append(string.Format(        
                "a {{color:{0}}}",        
                GetBrowserColor("PhoneAccentColor")));    
            head.Append("</style>");    
            head.Append(NotifyScript);    
            head.Append("</head>");     
            return head.ToString();
        }
        
        private static string GetBrowserColor(string sourceResource)
        {
            var color = (Color)Application.Current.Resources[sourceResource];    
            return "#" + color.ToString().Substring(3, 6);
        }

        public static string NotifyScript
        {
            get
            {
                return @"<script>        
                window.onload = function(){            
                    a = document.getElementsByTagName('a');            
                    for(var i=0; i < a.length; i++){                
                        var msg = a[i].href;                
                        a[i].onclick = function() {notify(msg);};            
                    }        
                }        
                function notify(msg) {        
                    window.external.Notify(msg);        
                    event.returnValue=false;        
                    return false;        
                }        
                </script>";
            }
        }

        public static string WrapHtml(
            string htmlSubString, 
            double viewportWidth)
        {
            var html = new StringBuilder();    
            html.Append("<html>");    
            html.Append(HtmlHeader(viewportWidth));    
            html.Append("<body>");    
            html.Append(htmlSubString);    
            html.Append("</body>");    
            html.Append("</html>");    
            return html.ToString();
        }


    }

}
